/**
 * 船模 编辑页
 *
 * 作者 ganjun
 * 日期 2024-11-13 19:03:49
 */

layui.use(['jquery','febs', 'form', 'validate', 'element', 'fileUpload', 'steps'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        element = layui.element,
        $view = $('#modelShip-process'),
        laydate = layui.laydate,
        fileUpload = layui.fileUpload,
        steps = layui.steps,
        validate = layui.validate;


    var status = 1;

    form.verify(validate);
    form.render();
    initFormValue();

    var stepData = {
        currentStep: status,
        steps: [
            {
                id: 1,
                done: true,
                desc: '开始'
            },
            {
                id: 2,
                done: false,
                desc: '上传图纸'
            },
            {
                id: 3,
                done: false,
                desc: '下料'
            },
            {
                id: 4,
                done: false,
                desc: '粗切'
            },
            {
                id: 5,
                done: false,
                desc: '精切'
            },
            {
                id: 6,
                done: false,
                desc: '修模'
            },
            {
                id: 7,
                done: false,
                desc: '油漆'
            },
            {
                id: 8,
                done: false,
                desc: '完工'
            },
        ]
    };

    /**
     * @param data  json 数据
     * @param ele   容器
     * @param currentStep 当前步骤
     * @param fields 定义展示字段名
     * @param isLeapfrog 是否越级  (默认false)
     * @param isShowTable 是否点击展示审批进度表格  (默认false)
     */
    steps.make({
        data: stepData.steps,
        ele: '#stepContent',
        currentStep: stepData.currentStep,
        handleIdxs:true,
        fields: {
            done: 'done', // true or false (true打对钩)
            id: 'id',
            desc: 'desc' // 内容
        }
    });

    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: ctx + 'upload/file',
        type: 2,//0-图片，1-视频，2-其他
        maxNum:5,
        defaultData: getFileList(),
        done: function (res) {
        },
    });

    function initFormValue() {
        if (modelShipData) {
            form.val("modelShip-process-form", {
                "id": modelShipData.id,
                "shipCode": modelShipData.shipCode,
                "projectCode": modelShipData.projectCode,
                "shipName": modelShipData.shipName,
                "source": modelShipData.source,
                "status": modelShipData.status,
                "finishTime": modelShipData.actuallyEnddate,
                "shipType": modelShipData.shipType,
                "rate": modelShipData.rate,
                "modelType": modelShipData.modelType,
                "applyDate": modelShipData.applyDate,
                "lengthOrigal": modelShipData.lengthOrigal,
                "lengthModel": modelShipData.lengthModel,
                "columnWidthOrigal": modelShipData.columnWidthOrigal,
                "columnWidthModel": modelShipData.columnWidthModel,
                "widthOrigal": modelShipData.widthOrigal,
                "widthModel": modelShipData.widthModel,
                "waterDeepOrigal": modelShipData.waterDeepOrigal,
                "waterDeepModel": modelShipData.waterDeepModel,
                "bodyDeepOrigal": modelShipData.bodyDeepOrigal,
                "bodyDeepModel": modelShipData.bodyDeepModel,
                "axieHightOrigal": modelShipData.axieHightOrigal,
                "axieHightModel": modelShipData.axieHightModel,
                "axieWidthOrigal": modelShipData.axieWidthOrigal,
                "axieWidthModel": modelShipData.axieWidthModel,
                "rectRate": modelShipData.rectRate,
                "blockNumber": modelShipData.blockNumber,
                "weight": modelShipData.weight,
                "headHeight": modelShipData.headHeight,
                "tailHeight": modelShipData.tailHeight,
                "headPush": modelShipData.headPush,
                "tailPush": modelShipData.tailPush,
                "memo": modelShipData.memo,
                "origShipCode": modelShipData.origShipCode,
                "statusName": modelShipData.statusName,
                "demandEnddate": modelShipData.demandEnddate,
                "actuallyStartdate": modelShipData.actuallyStartdate,
            });
        }
        if(test){
            form.val("modelShip-process-form", {
                "layoutPresRes":test.layoutPresRes,
                "layoutPresConcl":test.layoutPresConcl,
                "lbpStandard":test.lbpStandard,
                "lbpActual":test.lbpActual,
                "lbpError":test.lbpError,
                "lbpConcl":test.lbpConcl,
                "smwStandard":test.smwStandard,
                "smwActual":test.smwActual,
                "smwError":test.smwError,
                "smwConcl":test.smwConcl,
                "smahStandard":test.smahStandard,
                "smahActual":test.smahActual,
                "smahError":test.smahError,
                "smahConcl":test.smahConcl,
                "smawStandard":test.smawStandard,
                "smawActual":test.smawActual,
                "smawError":test.smawError,
                "smawConcl":test.smawConcl,
                "smtcbRes":test.smtcbRes,
                "smtcbConcl":test.smtcbConcl,
                "bslcpRes":test.bslcpRes,
                "bslcpConcl":test.bslcpConcl,
                "paintSmoothRes":test.paintSmoothRes,
                "paintSmoothConcl":test.paintSmoothConcl,
                "paintSurveyRes":test.paintSurveyRes,
                "paintSurveyConcl":test.paintSurveyConcl,
                "stationMarkRes":test.stationMarkRes,
                "stationMarkConcl":test.stationMarkConcl,
            });
        }

        if(uploadChartTime){
            form.val("modelShip-process-form", {
                "uploadChartTime":uploadChartTime,
            });
            if(modelShipData.actuallyStartdate){
                var workHour = calWorkTime(modelShipData.actuallyStartdate, uploadChartTime);
                $('#uploadChartDiv').text(workHour + '小时');
            }

        }

        if(cutTime){
            form.val("modelShip-process-form", {
                "cutTime":cutTime,
            });

            if(uploadChartTime){
                var workHour = calWorkTime(uploadChartTime, cutTime);
                $('#cutTimeDiv').text(workHour + '小时');
            }
        }

        if(cutOperator){
            form.val("modelShip-process-form", {
                "cutOperator":cutOperator,
            });
        }
        if(roughCutTime){
            form.val("modelShip-process-form", {
                "roughCutTime":roughCutTime,
            });

            if(cutTime){
                var workHour = calWorkTime(cutTime, roughCutTime);
                $('#roughCutTimeDiv').text(workHour + '小时');
            }
        }
        if(roughCutOperator){
            form.val("modelShip-process-form", {
                "roughCutOperator":roughCutOperator,
            });
        }
        if(fineCutTime){
            form.val("modelShip-process-form", {
                "fineCutTime":fineCutTime,
            });

            if(roughCutTime){
                var workHour = calWorkTime(roughCutTime, fineCutTime);
                $('#fineCutTimeDiv').text(workHour + '小时');
            }
        }

        if(fineCutOperator){
            form.val("modelShip-process-form", {
                "fineCutOperator":fineCutOperator,
            });
        }
        if(modRepairTime){
            form.val("modelShip-process-form", {
                "modRepairTime":modRepairTime,
            });

            if(roughCutTime){
                var workHour = calWorkTime(fineCutTime, modRepairTime);
                $('#modRepairTimeDiv').text(workHour + '小时');
            }
        }
        if(modRepairOperator){
            form.val("modelShip-process-form", {
                "modRepairOperator":modRepairOperator,
            });
        }
        if(paintTime){
            form.val("modelShip-process-form", {
                "paintTime":paintTime,
            });

            if(roughCutTime){
                var workHour = calWorkTime(modRepairTime, paintTime);
                $('#paintTimeDiv').text(workHour + '小时');
            }
        }
        if(paintOperator){
            form.val("modelShip-process-form", {
                "paintOperator":paintOperator,
            });
        }

        //默认打开的tab
        if(modelShipData && modelShipData.status){
            if(modelShipData.status > 8){
                status = 8;
            }else{
                status = modelShipData.status;
            }
        }
        changeTabs(status.toString());
    }

    $('.step-item-head').click(function(){
        var stepIndex = $(this).attr('step-index');
        changeTabs(stepIndex);
    });


    var curActiveTab;

    function changeTabs(stepIndex){
        if(stepIndex > status){
            febs.alert.warn('未到此步骤，无法操作！');
            return;
        }
        $('#base_field_one').hide();
        $('#base_field_two').hide();
        $('#base_field_three').hide();
        $('#base_field_four').hide();
        $('#base_field_five').hide();
        $('#base_field_six').hide();
        $('#base_field_seven').hide();
        $('#base_field_eight').hide();
        //控制提交按钮
        $('#submit').show();
        console.log('=======stepIndex====status====isPmRole', stepIndex, status, isPmRole);
        if(isPmRole === 1){
            if(stepIndex == '1' || stepIndex == '2' || stepIndex == '8'){
                $('#submit').show();
            }else{
                $('#submit').hide();
            }

        }else if(isPmRole === 2){
            if(stepIndex === '3' || stepIndex === '4' || stepIndex === '5' || stepIndex === '6' || stepIndex === '7'){
                $('#submit').show();
            }else{
                $('#submit').hide();
            }

        }else if(isPmRole === 3){
            $('#submit').show();
        }

        if(stepIndex != '2' && stepIndex != status){
            $('#submit').hide();
        }
        //控制tab页
        if(stepIndex === '1'){
            $('#base_field_one').show();
            curActiveTab = 'base_field_one';
        }else if(stepIndex === '2'){
            $('#base_field_two').show();
            curActiveTab = 'base_field_two';
        }else if(stepIndex === '3'){
            $('#base_field_three').show();
            curActiveTab = 'base_field_three';
        }else if(stepIndex === '4'){
            $('#base_field_four').show();
            curActiveTab = 'base_field_four';
        }else if(stepIndex === '5'){
            $('#base_field_five').show();
            curActiveTab = 'base_field_five';
        }else if(stepIndex === '6'){
            $('#base_field_six').show();
            curActiveTab = 'base_field_six';
        }else if(stepIndex === '7'){
            $('#base_field_seven').show();
            curActiveTab = 'base_field_seven';
        }else if(stepIndex === '8'){
            $('#base_field_eight').show();
            curActiveTab = 'base_field_eight';
        }
    }

    //计算时间差之间多少工时
    function calWorkTime(date1, date2){
        var val1 = new Date(date1).getTime();
        var val2 = new Date(date2).getTime();
        var diff = val2 - val1;
        //每天按8小时算
        var workHour = Math.round((diff / 1000 / 60 / 60 / 24)) * 8;
        return workHour;
    }


    form.on('submit(modelShip-process-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });



    function getFileList() {
        if (attachs && attachs.length>0) {
            return  attachs;
        }else {
            return  [];
        }
    }
    /**
     * 获取附件
     * @returns {*[]}
     */
    function getFileData(){
        var fileDatas = $view.find(".viewFile");
        var fileList = [];
        $.each(fileDatas,function (index, item){
            var data = {
                filePath: item.dataset.filepath,
                fileName: item.dataset.filename,
            }
            fileList.push(data);
        })
        return fileList;
    }

    function addOrUpdate(data) {
        //获取当前活动tab页的id，判断是第几步
        var curTab = curActiveTab;
        var url = '';
        var params = {};
        console.log('========curTab=', curTab);
        if(curTab === 'base_field_one'){
            if(data.status === '1'){
                url = 'modelShip/startProc';
                params.id = data.id;
                params.actuallyStartdate = data.actuallyStartdate;
                if(data.actuallyStartdate == null || data.actuallyStartdate == ''){
                    febs.alert.warn('开始时间不能为空');
                    return;
                }
                params.step = 1;
            }else{
                febs.alert.warn('当前为'+data.statusName+'状态，请到对应节点进行操作！');
                return;
            }
        }else if(curTab === 'base_field_two'){
            /*if(data.status === '2'){

            }else{
                febs.alert.warn('当前为'+data.statusName+'状态，请到对应节点进行操作！');
                return;
            }*/
            url = 'modelShip/uploadChart';
            params.id = data.id;
            params.uploadChartTime = data.uploadChartTime;
            params.step = 2;
            var fileList = [];
            fileList = getFileData();
            params.fileList = fileList;
            if(data.uploadChartTime == null || data.uploadChartTime == ''){
                febs.alert.warn('图纸完成时间不能为空');
                return;
            }
            if(fileList == null || fileList.length == 0){
                febs.alert.warn('图纸未上传，请先上传图纸');
                return;
            }
        }else if(curTab === 'base_field_three'){
            if(data.status === '3'){
                url = 'modelShip/cutting';
                params.id = data.id;
                params.cutTime = data.cutTime;
                params.cutOperator = data.cutOperator;
                params.step = 3;
                if(data.cutTime == null || data.cutTime == ''){
                    febs.alert.warn('下料完成时间不能为空');
                    return;
                }
            }else{
                febs.alert.warn('当前为'+data.statusName+'状态，请到对应节点进行操作！');
                return;
            }
        }else if(curTab === 'base_field_four'){
            if(data.status === '4'){
                url = 'modelShip/roughCut';
                params.id = data.id;
                params.roughCutTime = data.roughCutTime;
                params.roughCutOperator = data.roughCutOperator;
                params.step = 4;
                if(data.roughCutTime == null || data.roughCutTime == ''){
                    febs.alert.warn('粗切完成时间不能为空');
                    return;
                }
            }else{
                febs.alert.warn('当前为'+data.statusName+'状态，请到对应节点进行操作！');
                return;
            }
        }else if(curTab === 'base_field_five'){
            if(data.status === '5'){
                url = 'modelShip/fineCut';
                params.id = data.id;
                params.fineCutTime = data.fineCutTime;
                params.fineCutOperator = data.fineCutOperator;
                params.step = 5;
                if(data.fineCutTime == null || data.fineCutTime == ''){
                    febs.alert.warn('精切完成时间不能为空');
                    return;
                }
            }else{
                febs.alert.warn('当前为'+data.statusName+'状态，请到对应节点进行操作！');
                return;
            }

        }else if(curTab === 'base_field_six'){
            if(data.status === '6'){
                url = 'modelShip/modRepair';
                params.id = data.id;
                params.modRepairTime = data.modRepairTime;
                params.modRepairOperator = data.modRepairOperator;
                params.step = 6;
                if(data.modRepairTime == null || data.modRepairTime == ''){
                    febs.alert.warn('修模完成时间不能为空');
                    return;
                }
            }else{
                febs.alert.warn('当前为'+data.statusName+'状态，请到对应节点进行操作！');
                return;
            }

        }else if(curTab === 'base_field_seven'){
            if(data.status === '7'){
                url = 'modelShip/paint';
                params.id = data.id;
                params.paintTime = data.paintTime;
                params.paintOperator = data.paintOperator;
                params.step = 7;
                if(data.paintTime == null || data.paintTime == ''){
                    febs.alert.warn('油漆完成时间不能为空');
                    return;
                }
            }else{
                febs.alert.warn('当前为'+data.statusName+'状态，请到对应节点进行操作！');
                return;
            }
        }else if(curTab === 'base_field_eight'){
            if(data.status === '8'){
                url = 'modelShip/finish';
                params.id = data.id;
                params.finishTime = data.finishTime;
                if(data.finishTime == null || data.finishTime == ''){
                    febs.alert.warn('完成时间不能为空');
                    return;
                }
                params.finishOperator = data.finishOperator;
                params.step = 8;
                //检验表字段内容
                var testParams = {};

                testParams.layoutPresRes = data.layoutPresRes;
                testParams.layoutPresConcl = data.layoutPresConcl;
                testParams.lbpStandard = data.lbpStandard;
                testParams.lbpActual = data.lbpActual;
                testParams.lbpError = data.lbpError;
                testParams.lbpConcl = data.lbpConcl;
                testParams.smwStandard = data.smwStandard;
                testParams.smwActual = data.smwActual;
                testParams.smwError = data.smwError;
                testParams.smwConcl = data.smwConcl;
                testParams.smahStandard = data.smahStandard;
                testParams.smahActual = data.smahActual;
                testParams.smahError = data.smahError;
                testParams.smahConcl = data.smahConcl;
                testParams.smawStandard = data.smawStandard;
                testParams.smawActual = data.smawActual;
                testParams.smawError = data.smawError;
                testParams.smawConcl = data.smawConcl;
                testParams.smtcbRes = data.smtcbRes;
                testParams.smtcbConcl = data.smtcbConcl;
                testParams.bslcpRes = data.bslcpRes;
                testParams.bslcpConcl = data.bslcpConcl;
                testParams.paintSmoothRes = data.paintSmoothRes;
                testParams.paintSmoothConcl = data.paintSmoothConcl;
                testParams.paintSurveyRes = data.paintSurveyRes;
                testParams.paintSurveyConcl = data.paintSurveyConcl;
                testParams.stationMarkRes = data.stationMarkRes;
                testParams.stationMarkConcl = data.stationMarkConcl;

                params.test = testParams;
            }else{
                febs.alert.warn('当前为'+data.statusName+'状态，请到对应节点进行操作！');
                return;
            }
        }else{
            febs.alert.warn('当前为'+data.statusName+'状态，不能操作！');
            return;
        }
        febs.modal.confirm("提示！","是否提交进入下一步加工流程？",function (index){
            if (data.id && data.id.length > 0) {
                febs.postArray(ctx + url, params, function () {
                    layer.closeAll();
                    febs.alert.success('提交成功');
                    var currentUrl = window.location.hash.replace('#', '');
                    febs.view.tab.del(currentUrl);
                    febs.view.tab.change("/modelShip");
                    $('#febs-modelShip').find('#query').trigger('click');
                });
            }
        });
    }

    //实际开始时间
    if(status === 1){
        laydate.render({
            elem: "#actuallyStartdate",
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            disabledDate: function(date, type){
                return date.getTime() > Date.now();
            },

        });
    }



    //图纸上传完成时间
    if(status === 2){
        laydate.render({
            elem: "#uploadChartTime",
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            disabledDate: function(date, type){
                return date.getTime() > Date.now();
            },
            min: $('#actuallyStartdate').val(),
            done: function(value){
                var beforeDate = $('#actuallyStartdate').val();
                var workHour = calWorkTime(beforeDate, value);
                $('#uploadChartDiv').text(workHour + '小时');
            },
        });
    }


    //下料完成时间
    if(status === 3){
        laydate.render({
            elem: "#cutTime",
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            disabledDate: function(date, type){
                return date.getTime() > Date.now();
            },
            done: function(value){
                var beforeDate = $('#uploadChartTime').val();
                var workHour = calWorkTime(beforeDate, value);
                $('#cutTimeDiv').text(workHour + '小时');
            },
        });
    }


    //粗切完成时间
    if(status === 4){
        laydate.render({
            elem: "#roughCutTime",
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            disabledDate: function(date, type){
                return date.getTime() > Date.now();
            },
            done: function(value){
                var beforeDate = $('#cutTime').val();
                var workHour = calWorkTime(beforeDate, value);
                $('#roughCutTimeDiv').text(workHour + '小时');
            },
        });
    }


    //精切完成时间
    if(status === 5){
        laydate.render({
            elem: "#fineCutTime",
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            disabledDate: function(date, type){
                return date.getTime() > Date.now();
            },
            done: function(value){
                var beforeDate = $('#roughCutTime').val();
                var workHour = calWorkTime(beforeDate, value);
                $('#fineCutTimeDiv').text(workHour + '小时');
            },
        });
    }


    //修模完成时间
    if(status === 6){
        laydate.render({
            elem: "#modRepairTime",
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            disabledDate: function(date, type){
                return date.getTime() > Date.now();
            },
            done: function(value){
                var beforeDate = $('#fineCutTime').val();
                var workHour = calWorkTime(beforeDate, value);
                $('#modRepairTimeDiv').text(workHour + '小时');
            },
        });
    }


    //油漆完成时间
    if(status === 7){
        laydate.render({
            elem: "#paintTime",
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            disabledDate: function(date, type){
                return date.getTime() > Date.now();
            },
            done: function(value){
                var beforeDate = $('#modRepairTime').val();
                var workHour = calWorkTime(beforeDate, value);
                $('#paintTimeDiv').text(workHour + '小时');
            },
        });
    }


    //完成时间
    if(status === 8){
        laydate.render({
            elem: "#finishTime",
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            disabledDate: function(date, type){
                return date.getTime() > Date.now();
            },
        });
    }


    //操作流程页面
    $('#showProcs').click(function(){
        febs.modal.open('流程操作记录', 'modelShip/toProcsPage/' + modelShipData.id, {
            // area: $(window).width() <= 750 ? ['60%', '60%'] : ['60%', '60%'],
            area: ['400px', '500px'],
            btn: ['关闭'],
            btn2: function () {
                layer.closeAll();
            },
        });
    });

//返回按钮
    $('#reset').click(function () {
        closeCur();
    });

    function closeCur(){
        var currentUrl = window.location.hash.replace('#', '');
        febs.view.tab.del(currentUrl);
        febs.view.tab.change("/modelShip");
        $('#febs-modelShip').find('#query').trigger('click');
    }
});